#!/bin/sh

#
# Test that when the expand flag is set on the final partition, it expands.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
define(EFI_TYPE, "c12a7328-f81f-11d2-ba4b-00a0c93ec93b")
define(LINUX_ROOT_ARM_TYPE, "69DAD710-2CE4-4E3C-B16C-21A1D49ABED3")
define(LINUX_TYPE, "0FC63DAF-8483-4772-8E79-3D69D8477DE4")

define(DISK_UUID, "b443fbeb-2c93-481b-88b3-0ecb0aeba911")
define(EFI_PART_UUID, "5278721d-0089-4768-85df-b8f1b97e6684")
define(ROOTFS_A_PART_UUID, "fcc205c8-2f1c-4dcd-bef4-7b209aa15cca")
define(ROOTFS_B_PART_UUID, "4E69D2CD-028F-40CE-BBEE-94FB353B424C")
define(APP_PART_UUID, "9558571B-1DFC-4C3F-8DB4-A8A564FB99A4")

# Numbers don't matter for the test so long as the MBR is right
define(BOOT_PART_OFFSET, 1024)
define(BOOT_PART_COUNT, 1024)
define(ROOTFS_A_PART_OFFSET, 2048)
define(ROOTFS_A_PART_COUNT, 2048)
define(ROOTFS_B_PART_OFFSET, 4096)
define(ROOTFS_B_PART_COUNT, 2048)
define(APP_PART_OFFSET, 8192)
define(APP_PART_COUNT, 4096)
# define(APP_PART_COUNT, 12288) # expanded size

gpt gpt-a {
    guid = \${DISK_UUID}
    partition 0 {
        block-offset = \${BOOT_PART_OFFSET}
        block-count = \${BOOT_PART_COUNT}
        type = \${EFI_TYPE}
        guid = \${EFI_PART_UUID}
        name = "efi-part.vfat"
    }
    partition 1 {
        block-offset = \${ROOTFS_A_PART_OFFSET}
        block-count = \${ROOTFS_A_PART_COUNT}
        type = \${LINUX_ROOT_ARM_TYPE}
        guid = \${ROOTFS_A_PART_UUID}
        name = "rootfs.a.ext2"
    }
    partition 2 {
        block-offset = \${ROOTFS_B_PART_OFFSET}
        block-count = \${ROOTFS_B_PART_COUNT}
        type = \${LINUX_ROOT_ARM_TYPE}
        guid = \${ROOTFS_B_PART_UUID}
        name = "rootfs.b.ext2"
    }
    partition 3 {
        block-offset = \${APP_PART_OFFSET}
        block-count = \${APP_PART_COUNT}
        type = \${LINUX_TYPE}
        guid = \${APP_PART_UUID}
        name = "app"
        expand = true
    }
}
task complete {
	on-init {
                gpt_write(gpt-a)
        }
}
EOF

# Create the expected output from sfdisk by running ./166_gpt_expand-generate.sh on
# Linux.
base64_decodez >$WORK/expected-primary-gpt.img <<EOF
H4sIABNvu10AA+3QTSiDcRwH8N8WkYNtJ4dZNidpTCunmdcYmcUiDrtMtjylrGdLk0htigMOONjJ
isJlyQ5easVBRnMYRS4OdlutlIPa6vGXZ4dd1EOJ+n6e/vV/+z7fp4cI/jM5ZQRBkLGZrkF6emi/
s6tH299mHySSkYPtPAcixo8TmXgj/9bqfERcp7Md0TW9unvxsPy6LL2nkovH8+KwDQhx6d8Dv63G
W3tW9ZZUnfTSVrw5btLwfvsSjVsWno5ejufcQSr6vCeUFOZc5CaO6shDTuLJR/U0xXacbCaN8v6R
S+mbbAcTuvBdKHZ7WXyeqzQk+mKvM9pQ2HFFpWK/ojDH0yR7fKzTy7qdbLjIz9bGH/YnkpxtVX7T
eppZzzaa2q2kFPsrvuof/XZ/xHyhCAR5y8q0mXuwzKbUwyMbOU2LdTm6uzOW3dwmrdhvKMw52Z/3
SOwCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIC/7R2zs5eOAEIAAA==
EOF
base64_decodez >$WORK/expected-secondary-gpt.img <<EOF
H4sIABNvu10AA+3QTyiDYRwH8N9E5GDbyWGWjYs0JuU08zdeMoulOOzglS1vKevd0iRSpjjggIOd
rKY4SXbwp1YcZDSHUeTisttKKQfaah48DruoWcnh+3l66nl+z/Pr+/RUuatPy19j6uMe2oo0RUxa
2WtbojFh4fHw+WjW6aN8+pQupAwOcpJENeQikWTyUC1NsorIVtlR3T1IcUOjdX9cH7j1h28uCs5S
ZcZob/hlWucP2C+piOcrM/tkmmDDwzLdLFtk00Fetq/PMT8ak6yredctJ0/ryQZTm4VUPL/0p/yR
X+fvmc+V8z5ZWJkyS/fCTFwzOLSR0jZblkM726PJzSDpeL4xs09kP+/KMgsAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAD+r47Obl1fq22ASEF2tq8cDr591PV1X+cKfq/iu4HXE8n20JpB07V4
UHJVnNhVK3h9jk9rfzryF++H3LwDhhS2pQBCAAA=
EOF
cp $WORK/expected-primary-gpt.img $WORK/unexpanded.img
dd if=$WORK/expected-secondary-gpt.img of=$WORK/unexpanded.img bs=512 seek=12289 conv=notrunc

base64_decodez >$WORK/expected-primary-gpt2.img <<EOF
H4sIAEOfxV0AA+3TTyiDYRwH8N/WRA62nRxmGSdprJTTzN+YZdZaxGGXV7a8pax3S5NImeKAAw52
sqJwkezgT604yGgOo0jhspPVSjmorV6P9jrMQY0S9f28PfX8+f2eb+9bLxH8Z3JKiaIoYzPRnn93
73Z7h0Vnb3H0EMnIyXY0ioTi/UQmVXzcWimtH6R1Mt0WXtJrOmf3Si6Kk1tquXQ+JY0ni8H5jdeB
X1blrT4uf42rD7toLdoYNWoFv2OOhswzj/vPB5PuACmydWJhbp+L3MRTDXmII4F8VEujbIdjs/yo
bu74hL7BtjtcEboORq7OCk4yZYZYd+RlXBcMOc+pSMpX5vYJNMIeH8v0smyODRf52bruh/mxOG9b
lF82H6WW0/XGViuppPzSr/IHvp2/YzpVTgcE88KYib81TyQ0ff0rGW2TdT68uTGYXl0nXbbu/tPf
zbEv78kzCwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/rY3o2m94gBCAAA=
EOF
base64_decodez >$WORK/expected-secondary-gpt2.img <<EOF
H4sIAEOfxV0AA+3bPUiCQRgH8McoiobUqcEka4qwhKDJ7JMyyUQkyMHljZReCJJXCSOKIIMaqqEa
ckooqCkihz5AqCGysMGCIkgXlxKCoCFQ0KtucQlMiIb/7zju7uGOP3f7NXgaz2o/YsqTAdqKtEf0
aslnW6Ix40Li6O141uWnUvqSLac8TnKRSE3kJoEk8lIzTbKKwGaFUdw/ikltm+VgvC54FwjfXpad
Z2p00cHw+7QmEHRcUQXPl+efk2iCNS/L9LBsgXUn+di6pcj8aEy0rJbcdJ6+rqdb9d1mUvD86p/y
R36dv2+4kM/7JePKlEF8MM4kVcP2jYy6w7wc2t0ZTW9uk+Z735M1/5zAXt5dYBYAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAD8X719Jo21yzZEJCMHW8ft0efPepb/N5fxffV8jPN6Kt0TWtOq
+hcPq64rU3vKBK/P8f5i0jn+5AJQlBysljDEAEIAAA==
EOF
cp $WORK/expected-primary-gpt2.img $WORK/expanded.img
dd if=$WORK/expected-secondary-gpt2.img of=$WORK/expanded.img bs=512 seek=20703 conv=notrunc

# Create the firmware file, then "burn it"
$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Check that when creating a file and there's no size information that
# the partition will be the specified block_size
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete
cmp_bytes 6308864 $WORK/unexpanded.img $IMGFILE

# Repeat the test, but specify a max size so that the final partition is larger
rm $IMGFILE
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE --max-size=20736 -t complete
cmp $WORK/expanded.img $IMGFILE

# Repeat the test, but now apply to a large regular file. This is a common qemu
# use case.
rm $IMGFILE
dd if=/dev/zero of=$IMGFILE seek=20736 count=0
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete
cmp $WORK/expanded.img $IMGFILE

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
